
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import org.json.me.JSONArray;
import org.json.me.JSONObject;

public class f_readsong extends SongListView implements CommandListener {

    private Command CMD_BACK;
    private Command CMD_SEARCH;
    private Command CMD_ADD;
    private Command CMD_DELETE;
    private Command CMD_PLAYER;
    private Playlist playlist;
    private int type;

    public f_readsong(int type) {
        this.type = type;
        CMD_BACK = new Command("Back", Command.BACK, 0);
        CMD_SEARCH = new Command("Search", Command.SCREEN, 0);
        CMD_ADD = new Command("Add to", Command.SCREEN, 0);
        CMD_DELETE = new Command("Delete", Command.SCREEN, 0);
        CMD_PLAYER = new Command("Now Playing", Command.SCREEN, 0);
        if (type == 0) {
            this.addCommand(CMD_BACK);
            this.addCommand(CMD_SEARCH);
            this.addCommand(CMD_ADD);
            this.addCommand(CMD_PLAYER);
        } else if (type == 1) {
            this.addCommand(CMD_BACK);
            this.addCommand(CMD_DELETE);
        } else if (type == 2) {
            this.addCommand(CMD_BACK);
            this.addCommand(CMD_ADD);
        }
        this.setCommandListener(this);
    }

    public void setPlaylist(Playlist pl) {
        this.playlist = pl;
    }

    public void load(Song[] song) {
        this.loadItems(song);
    }

    public void load(String url) {
        Song[] tmp_song;
        try {
            HttpConnection hc = (HttpConnection) Connector.open(AppManager.SERVER + url);
            InputStream is = hc.openInputStream();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            char[] data = new char[1];
            String str_json = "";
            while (isr.read(data) != -1) {
                str_json += new String(data);
            }
            hc.close();
            JSONArray ja = new JSONArray(str_json);
            tmp_song = new Song[ja.length() - 1];
            JSONObject jo;
            for (int i = 0; i < ja.length() - 1; i++) {
                jo = ja.getJSONObject(i);
                tmp_song[i] = new Song(jo.getString("spl_id"), jo.getString("spl_name"), jo.getString("casi_name"), 0, jo.getString("url"));
            }
            this.loadItems(tmp_song);
        } catch (Exception ex) {
        }
    }

    public void onSelect(int selectedIndex) {
        if (type == 0) {
            try {
                HttpConnection hc = (HttpConnection) Connector.open(AppManager.SERVER + "count.php?type=song&id=" + songs[selectedIndex].code);
                hc.getResponseCode();
                hc.close();
            } catch (Exception ex) {
            }
            AppManager.F_PLAYER.load(this.songs[selectedIndex]);
            AppManager.F_PLAYER.playStart();
            AppManager.open(AppManager.F_PLAYER);
        } else if (type == 2) {
            AppManager.F_PLAYER.playIndex(selectedIndex);
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c == CMD_BACK) {
            AppManager.back();
        } else if (c == CMD_SEARCH) {
            AppManager.open(new Search());
        } else if (c == CMD_PLAYER) {
            AppManager.open(AppManager.F_PLAYER);
        }
    }

    public void onLeftRight() {
        AppManager.swap();
    }
}
